package com.seaengine.proxy
{
	import com.seaengine.GameConfig;
	import com.seaengine.constData.Enums;
	import com.seaengine.resourcemanager.ResSystem;
	import com.seaengine.util.StringUtils;
	
	import flash.utils.setTimeout;

	public class CharacterKindConfigProxy
	{
		public var m_ID_Map:Array;	
		
		private static var instance:CharacterKindConfigProxy;
		public static const MAX_ROLE_COUNT:int = 500;
		public static const MAX_EFFECT_COUNT:int = 600;
		public static const MAX_WEAPON_COUNT:int = 700;
		
		public static const TYPE_CHARACTER:String = "character";
		public static const TYPE_EFFECT:String = "effect";
		public static const TYPE_WEAPON:String = "weapon";
		
		public function CharacterKindConfigProxy()
		{
			if (instance) return;
		}
		
		public static function getInstance():CharacterKindConfigProxy
		{
			if (!instance) instance = new CharacterKindConfigProxy();
			
			return instance;
		}
		public function init():void
		{
			ResSystem.getInstance().getRes(GameConfig.m_ResConfig + "role/roleIdMap.xml", onInitBack);
			
		}
		private function onInitBack(fileName:String, data:Object):void
		{
			m_ID_Map = new Array();
			var xml:XMLList = (data as XML).item;
			var len:int = xml.length();
			for (var i:int = 0; i < len; i++)
			{
				//trace("id:"+xml[i].id + " xml:"+ xml[i].xml)
				m_ID_Map.push({id:xml[i].id,xml:xml[i].xml});
			}
			setTimeout(function():void{ResSystem.getInstance().unloadRes(GameConfig.m_ResConfig + "role/roleIdMap.xml");},1000);
		}

		public function getIDByParty(_party:uint,_sex:uint):int
		{
			switch(_party)
			{
				case Enums.PARTY_NONE:
						if(_sex==1)
							return getTypeIDByXMLName("xin_shou_nan_0.xml");
						else
							return getTypeIDByXMLName("xin_shou_nv_0.xml");
					break;
				case Enums.PARTY_MOJIAO:
						if(_sex==1)
							return getTypeIDByXMLName("mo_jiao_nan_0.xml");
						else
							return getTypeIDByXMLName("mo_jiao_nv_0.xml");
					break;
				case Enums.PARTY_DAQIMEN: //大旗门只有男
					return getTypeIDByXMLName("tie_xue_nan_0.xml");
					break;
				case Enums.PARTY_SHENSHUI: //神水宫只有女
					return getTypeIDByXMLName("shen_shui_nv_0.xml");
					break;
				case Enums.PARTY_WUJIMEN: 
						if(_sex==1)
							return getTypeIDByXMLName("wu_ji_nan_0.xml");
						else
							return getTypeIDByXMLName("wu_ji_nv_0.xml");
					break;
				case Enums.PARTY_TANGMEN:
						if(_sex==1)
							return getTypeIDByXMLName("tang_men_nan_0.xml");
						else
							return getTypeIDByXMLName("tang_men_nv_0.xml");
					break;
				case Enums.PARTY_SHENJIAN:
						if(_sex==1)
							return getTypeIDByXMLName("shen_jian_nan_0.xml");
						else
							return getTypeIDByXMLName("shen_jian_nv_0.xml");
					break;
				default:
					return 1;
					break;
			}
			
		}
//		public function getHeadNameByParty():String
//		{
//			var type:uint = PlayerAttribute.getInstance().m_CommonInfo._party;
//			switch(type)
//			{
//				case Enums.PARTY_NONE:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return taskSystem.getInstance().npcCustomTalk[1001].m_head;
//					else
//						return taskSystem.getInstance().npcCustomTalk[1002].m_head;
//					break;
//				case Enums.PARTY_MOJIAO:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return taskSystem.getInstance().npcCustomTalk[1003].m_head;
//					else
//						return taskSystem.getInstance().npcCustomTalk[1004].m_head;
//					break;
//				case Enums.PARTY_DAQIMEN:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return taskSystem.getInstance().npcCustomTalk[1005].m_head;
//					else
//						return taskSystem.getInstance().npcCustomTalk[1006].m_head;
//					break;
//				case Enums.PARTY_SHENSHUI:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return taskSystem.getInstance().npcCustomTalk[1007].m_head;
//					else
//						return taskSystem.getInstance().npcCustomTalk[1008].m_head;
//					break;
//				case Enums.PARTY_SHENJIAN: 
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return taskSystem.getInstance().npcCustomTalk[1009].m_head;
//					else
//						return taskSystem.getInstance().npcCustomTalk[1010].m_head;
//					break;
//				case Enums.PARTY_WUJIMEN:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return taskSystem.getInstance().npcCustomTalk[1011].m_head;
//					else
//						return taskSystem.getInstance().npcCustomTalk[1012].m_head;
//					break;
//				case Enums.PARTY_TANGMEN:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return taskSystem.getInstance().npcCustomTalk[1013].m_head;
//					else
//						return taskSystem.getInstance().npcCustomTalk[1014].m_head;
//					break;
//				default:
//					return taskSystem.getInstance().npcCustomTalk[1002].m_head;
//					break;
//			}
//			
//		}
		
//		public function getFuncHeadNameByParty():String
//		{
//			var type:uint = PlayerAttribute.getInstance().m_CommonInfo._party;
//			switch(type)
//			{
//				case 1:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return "xinshounan";
//					else
//						return "xinshounv";
//					break;
//				case 2:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return "mojiaonan";
//					else
//						return "mojiaonv";
//					break;
//				case 3:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return "tiexuenan";
//					else
//						return "tiexuenan";
//					break;
//				case 4:
//					if(PlayerAttribute.getInstance().m_CommonInfo._sex==1)
//						return "shenshuinv";
//					else
//						return "shenshuinv";
//					break;
//				default:
//					return "xinshounan";
//					break;
//			}
//			
//		}
//		
		public function checkIsNormalTypeID(_id:int):Boolean
		{
			//var npcObj:NPCVo=taskSystem.getInstance().npcCustomTalk[_id];
			var tempXmlName:String=getXMLNameByTypeID(_id);
			return StringUtils.endsWith(tempXmlName,"_n.xml");
		}
		
		public function getNormalTypeIDByTypeID(_id:int):int
		{
			//var npcObj:NPCVo=taskSystem.getInstance().npcCustomTalk[_id];
			var tempXmlName:String=getXMLNameByTypeID(_id);
			tempXmlName=StringUtils.replace(tempXmlName,".xml","_n.xml");
			for( var i:int = 0 ; i < m_ID_Map.length; i++ )
			{
				var strTemp:String=m_ID_Map[i].xml;
				if("role/"+strTemp.toLowerCase() == tempXmlName.toLowerCase())
				{					
					return m_ID_Map[i].id;
				}			
			}
			return _id;		 
		}
		
		public function getTypeIDByNormalTypeID(_normalId:int):int
		{
			//var npcObj:NPCVo=taskSystem.getInstance().npcCustomTalk[_id];
			var tempXmlName:String=getXMLNameByTypeID(_normalId);
			tempXmlName=StringUtils.replace(tempXmlName,"_n.xml",".xml");
			for( var i:int = 0 ; i < m_ID_Map.length; i++ )
			{
				var strTemp:String=m_ID_Map[i].xml;
				if("role/"+strTemp.toLowerCase() == tempXmlName.toLowerCase())
				{					
					return m_ID_Map[i].id;
				}			
			}
			return _normalId;		 
		}
		
		public function getXMLNameByTypeID(_id:int):String
		{
			//var npcObj:NPCVo=taskSystem.getInstance().npcCustomTalk[_id];
			/*
			if(npcObj==null){
				ErrorOutput.error("npc名字查找","该NPCID:"+_id+"不存在");
				return "";	
			}else{
				return "role/" + npcObj.m_image.split("swf").join("xml");
			}*/
			for( var i:int = 0 ; i < m_ID_Map.length; i++ )
			{
				if( m_ID_Map[i].id == _id )
				{					
					return "role/" + m_ID_Map[i].xml;
				}			
			}
			return "";		 
		}
		public function getTypeIDByXMLName(_fileName:String):int
		{
			var str:String = StringUtils.extractFileNameFromUrl(_fileName);
//			{
//				var npcObj:NPCVo=taskSystem.getInstance().npcCustomTalk[npcList[i]];
//				if(npcObj.m_xml==str)
//				{
//					return npcObj.m_id;
//				}
//			}

			for(var i:int=0;i<m_ID_Map.length;i++){
				var temp:String=m_ID_Map[i].xml;
				if(temp.toLowerCase()==str.toLowerCase()){
					return m_ID_Map[i].id;
				}
			}
			return -1;		
		}
		public function getTypeIDByFileName(_fileName:String):int
		{
			var xmlUrl:String=_fileName.split(".swf").join(".xml")
			return getTypeIDByXMLName(xmlUrl);
		}
		public function getKindByID(_id:int):String
		{
			return getKindByXMLName(getXMLNameByTypeID(_id));		
		}
		public function getKindByXMLName(_fileName:String):String
		{
			if(_fileName.indexOf("_w.xml") > -1)
				return TYPE_WEAPON;		
			if(_fileName.indexOf("_effect.xml") > -1)
				return TYPE_EFFECT;		
			
			return TYPE_CHARACTER;	
		}
		
	}
}